<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Multi get (mget) API | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'docs-multi-get.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/docs-multi-get.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/docs-multi-get.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/docs-multi-get.html" rel="nofollow" target="_blank">../en/docs-multi-get.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="rest-apis.html">REST APIs</a></span>
»
<span class="breadcrumb-link"><a href="docs.html">Document APIs</a></span>
»
<span class="breadcrumb-node">Multi get (mget) API</span>
</div>
<div class="navheader">
<span class="prev">
<a href="docs-update-by-query.html">« Update by query API</a>
</span>
<span class="next">
<a href="docs-bulk.html">Bulk API »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="docs-multi-get"></a>Multi get (mget) API<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h2>
</div></div></div>

<p>Retrieves multiple JSON documents by ID.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_mget
{
    "docs" : [
        {
            "_index" : "twitter",
            "_id" : "1"
        },
        {
            "_index" : "twitter",
            "_id" : "2"
        }
    ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1522.console"></div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="docs-multi-get-api-request"></a>Request<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h3>
</div></div></div>
<p><code class="literal">GET /_mget</code></p>
<p><code class="literal">GET /&lt;index&gt;/_mget</code></p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="docs-multi-get-api-desc"></a>Description<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h3>
</div></div></div>
<p>You use <code class="literal">mget</code> to retrieve multiple documents from one or more indices.
If you specify an index in the request URI, you only need to specify the document IDs in the request body.</p>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="mget-security"></a>Security<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h4>
</div></div></div>
<p>See <a class="xref" href="url-access-control.html" title="URL-based access control">URL-based access control</a>.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="multi-get-partial-responses"></a>Partial responses<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h4>
</div></div></div>
<p>To ensure fast responses, the multi get API responds with partial results if one or more shards fail.
See <a class="xref" href="docs-replication.html#shard-failures" title="Shard failures">Shard failures</a> for more information.</p>
</div>

</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="docs-multi-get-api-path-params"></a>Path parameters<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">&lt;index&gt;</code>
</span>
</dt>
<dd>
(Optional, string) Name of the index to retrieve documents from when <code class="literal">ids</code> are specified,
or when a document in the <code class="literal">docs</code> array does not specify an index.
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="docs-multi-get-api-query-params"></a>Query parameters<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">preference</code>
</span>
</dt>
<dd>
(Optional, string) Specifies the node or shard the operation should be
performed on. Random by default.
</dd>
<dt>
<span class="term">
<code class="literal">realtime</code>
</span>
</dt>
<dd>
(Optional, boolean) If <code class="literal">true</code>, the request is real-time as opposed to near-real-time.
Defaults to <code class="literal">true</code>. See <a class="xref" href="docs-get.html#realtime" title="Realtime">Realtime</a>.
</dd>
<dt>
<span class="term">
<code class="literal">refresh</code>
</span>
</dt>
<dd>
(Optional, Boolean) If <code class="literal">true</code>, the request refreshes relevant shards before
retrieving documents. Defaults to <code class="literal">false</code>.
</dd>
<dt>
<span class="term">
<code class="literal">routing</code>
</span>
</dt>
<dd>
(Optional, string) Target the specified primary shard.
</dd>
<dt>
<span class="term">
<code class="literal">stored_fields</code>
</span>
</dt>
<dd>
(Optional, boolean) If <code class="literal">true</code>, retrieves the document fields stored in the
index rather than the document <code class="literal">_source</code>. Defaults to <code class="literal">false</code>.
</dd>
<dt>
<span class="term">
<code class="literal">_source</code>
</span>
</dt>
<dd>
(Optional, string) True or false to return the <code class="literal">_source</code> field or not, or a
list of fields to return.
</dd>
<dt>
<span class="term">
<code class="literal">_source_excludes</code>
</span>
</dt>
<dd>
<p>
(Optional, string)
A comma-separated list of <a class="xref" href="mapping-source-field.html" title="_source field">source fields</a> to exclude from
the response.
</p>
<p>You can also use this parameter to exclude fields from the subset specified in
<code class="literal">_source_includes</code> query parameter.</p>
<p>If the <code class="literal">_source</code> parameter is <code class="literal">false</code>, this parameter is ignored.</p>
</dd>
<dt>
<span class="term">
<code class="literal">_source_includes</code>
</span>
</dt>
<dd>
<p>
(Optional, string)
A comma-separated list of <a class="xref" href="mapping-source-field.html" title="_source field">source fields</a> to
include in the response.
</p>
<p>If this parameter is specified, only these source fields are returned. You can
exclude fields from this subset using the <code class="literal">_source_excludes</code> query parameter.</p>
<p>If the <code class="literal">_source</code> parameter is <code class="literal">false</code>, this parameter is ignored.</p>
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="docs-multi-get-api-request-body"></a>Request body<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">docs</code>
</span>
</dt>
<dd>
<p>
(Optional, array) The documents you want to retrieve.
Required if no index is specified in the request URI.
You can specify the following attributes for each
document:
</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">_id</code>
</span>
</dt>
<dd>
(Required, string) The unique document ID.
</dd>
<dt>
<span class="term">
<code class="literal">_index</code>
</span>
</dt>
<dd>
(Optional, string)
The index that contains the document.
Required if no index is specified in the request URI.
</dd>
<dt>
<span class="term">
<code class="literal">_routing</code>
</span>
</dt>
<dd>
(Optional, string) The key for the primary shard the document resides on.
Required if routing is used during indexing.
</dd>
<dt>
<span class="term">
<code class="literal">_source</code>
</span>
</dt>
<dd>
<p>
(Optional, boolean) If <code class="literal">false</code>,  excludes all <code class="literal">_source</code> fields. Defaults to <code class="literal">true</code>.
</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">source_include</code>
</span>
</dt>
<dd>
(Optional, array) The fields to extract and return from the <code class="literal">_source</code> field.
</dd>
<dt>
<span class="term">
<code class="literal">source_exclude</code>
</span>
</dt>
<dd>
(Optional, array) The fields to exclude from the returned <code class="literal">_source</code> field.
</dd>
</dl>
</div>
</dd>
<dt>
<span class="term">
<code class="literal">_stored_fields</code>
</span>
</dt>
<dd>
(Optional, array) The stored fields you want to retrieve.
</dd>
</dl>
</div>
</dd>
<dt>
<span class="term">
<code class="literal">ids</code>
</span>
</dt>
<dd>
(Optional, array) The IDs of the documents you want to retrieve.
Allowed when the index is specified in the request URI.
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="multi-get-api-response-body"></a>Response body<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The response includes a <code class="literal">docs</code> array that contains the documents in the order specified in the request.
The structure of the returned documents is similar to that returned by the <a class="xref" href="docs-get.html" title="Get API">get</a> API.
If there is a failure getting a particular document, the error is included in place of the document.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="docs-multi-get-api-example"></a>Examples<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="mget-ids"></a>Get documents by ID<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h4>
</div></div></div>
<p>If you specify an index in the request URI, only the document IDs are required in the request body:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /twitter/_mget
{
    "docs" : [
        {
            "_type" : "_doc",
            "_id" : "1"
        },
        {
            "_type" : "_doc",
            "_id" : "2"
        }
    ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1523.console"></div>
<p>And type:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /test/_doc/_mget
{
    "docs" : [
        {
            "_id" : "1"
        },
        {
            "_id" : "2"
        }
    ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1524.console"></div>
<p>You can use the <code class="literal">ids</code> element to simplify the request:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /twitter/_mget
{
    "ids" : ["1", "2"]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1525.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="mget-source-filtering"></a>Filter source fields<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h4>
</div></div></div>
<p>By default, the <code class="literal">_source</code> field is returned for every document (if stored).
Use the <code class="literal">_source</code> and <code class="literal">_source_include</code> or <code class="literal">source_exclude</code> attributes to
filter what fields are returned for a particular document.
You can include the <code class="literal">_source</code>, <code class="literal">_source_includes</code>, and <code class="literal">_source_excludes</code> query parameters in the
request URI to specify the defaults to use when there are no per-document instructions.</p>
<p>For example, the following request sets <code class="literal">_source</code> to false for document 1 to exclude the
source entirely, retrieves <code class="literal">field3</code> and <code class="literal">field4</code> from document 2, and retrieves the <code class="literal">user</code> field
from document 3 but filters out the <code class="literal">user.location</code> field.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "_source" : false
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2",
            "_source" : ["field3", "field4"]
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "3",
            "_source" : {
                "include": ["user"],
                "exclude": ["user.location"]
            }
        }
    ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1526.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="mget-fields"></a>Get stored fields<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h4>
</div></div></div>
<p>Use the <code class="literal">stored_fields</code> attribute to specify the set of stored fields you want
to retrieve. Any requested fields that are not stored are ignored.
You can include the <code class="literal">stored_fields</code> query parameter in the request URI to specify the defaults
to use when there are no per-document instructions.</p>
<p>For example, the following request retrieves <code class="literal">field1</code> and <code class="literal">field2</code> from document 1, and
<code class="literal">field3</code> and `field4`from document 2:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "stored_fields" : ["field1", "field2"]
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2",
            "stored_fields" : ["field3", "field4"]
        }
    ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1527.console"></div>
<p>The following request retrieves <code class="literal">field1</code> and <code class="literal">field2</code> from all documents by default.
These default fields are returned for document 1, but
overridden to return <code class="literal">field3</code> and <code class="literal">field4</code> for document 2.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /test/_doc/_mget?stored_fields=field1,field2
{
    "docs" : [
        {
            "_id" : "1"
        },
        {
            "_id" : "2",
            "stored_fields" : ["field3", "field4"]
        }
    ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1528.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="mget-routing"></a>Specify document routing<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/docs/multi-get.asciidoc">edit</a>
</h4>
</div></div></div>
<p>If routing is used during indexing, you need to specify the routing value to retrieve documents.
For example, the following request fetches <code class="literal">test/_doc/2</code>  from the shard corresponding to routing key <code class="literal">key1</code>,
and fetches <code class="literal">test/_doc/1</code> from the shard corresponding to routing key <code class="literal">key2</code>.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_mget?routing=key1
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "routing" : "key2"
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2"
        }
    ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1529.console"></div>
</div>

</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="docs-update-by-query.html">« Update by query API</a>
</span>
<span class="next">
<a href="docs-bulk.html">Bulk API »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>